package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Level;

public class LevelDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(Level level) {
		open();
		this.sql = "INSERT INTO Levels (description_level) VALUES (?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, level.getDescriptionLevel());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void update(Level level) {
		open();
		this.sql = "UPDATE Levels SET description_level = ? WHERE id_level = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, level.getDescriptionLevel());
			this.statement.setInt(2, level.getIdLevel());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(Level level) {
		open();
		this.sql = "DELETE FROM Levels WHERE id_level = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, level.getIdLevel());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public Level search(Integer id_level) {
		open();
		this.sql = "SELECT * FROM Levels WHERE id_level = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_level);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new Level(result.getInt("id_level"),
						result.getString("description_level"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<Level> list() {
		open();
		this.sql = "SELECT * FROM Levels";
		ArrayList<Level> levels = new ArrayList<Level>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				levels.add(new Level(result.getInt("id_level"), result
						.getString("description_level")));
			}
			return levels;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
